package com.lzl.api;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lzl.common.APIJsonResult;
import com.lzl.common.ConstantsEJS;
import com.lzl.exception.BusinessException;

@ControllerAdvice
public class GlobalExceptionHandler {

    private final Logger        logger          = LogManager.getLogger(this.getClass());

	@ExceptionHandler(value = Exception.class)
    @ResponseBody
    public <T> ResponseEntity<APIJsonResult> jsonErrorHandler(HttpServletRequest req, Exception e) throws Exception {
    	APIJsonResult result = new APIJsonResult<T>();
    	if(e instanceof BusinessException){
    		result.setMessage(e.getMessage());
    	}else{
    		result.setMessage(ConstantsEJS.SERVICE_RESULT_EXCEPTION_SYSERROR);
    	}
		logger.error(e);
    	
    	return new ResponseEntity<APIJsonResult>(result,HttpStatus.INTERNAL_SERVER_ERROR);
    }

}

